﻿using System;
using System.Net.Mail;
using System.Configuration;
using System.Net;

namespace BlogHunter.Util
{
    public class MailUtil
    {
        private static readonly string SMTPServer = ConfigurationSettings.AppSettings["SMTPServer"];
        private static readonly int SMTPPort = Convert.ToInt16(ConfigurationSettings.AppSettings["SMTPPort"]);
        private static readonly string SMTPHost = ConfigurationSettings.AppSettings["SMTPHost"];
        private static readonly string SMTPUser = ConfigurationSettings.AppSettings["SMTPUser"];
        private static readonly string SMTPPassword = ConfigurationSettings.AppSettings["SMTPPassword"];

        public static readonly string DefaultEmailAddressFrom = ConfigurationSettings.AppSettings["SMTPUser"];
        public static readonly string EmailReceiverList = ConfigurationSettings.AppSettings["EmailReceiverList"];

        /// <summary>
        /// SendSmtpMail
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public static void SendSmtpMail(string from, string to, string subject, string body)
        {
            using (MailMessage msgMail = new MailMessage())
            {
                msgMail.To.Add(to.Replace(";", ","));
                msgMail.From = new MailAddress(!String.IsNullOrEmpty(from) ? from : DefaultEmailAddressFrom);
                msgMail.Subject = subject;
                msgMail.IsBodyHtml = false;
                msgMail.Body = body;
                msgMail.Priority = MailPriority.High;

                SmtpClient client = GetSmtpClient();
                try
                {
                    client.Send(msgMail);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        /// <summary>
        /// SendSmtpHTMLMail
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public static void SendSmtpHTMLMail(string from, string to, string subject, string body)
        {
            using (MailMessage msgMail = new MailMessage())
            {
                msgMail.To.Add(to.Replace(";", ","));
                msgMail.From = new MailAddress(!String.IsNullOrEmpty(from) ? from : DefaultEmailAddressFrom);
                msgMail.Subject = subject;
                msgMail.IsBodyHtml = true;
                msgMail.Body = body;
                msgMail.Priority = MailPriority.High;

                SmtpClient client = GetSmtpClient();
                try
                {
                    client.Send(msgMail);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        /// <summary>
        /// SendSmtpAttachmentsMail
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="cc"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="attachments"></param>
        public static void SendSmtpAttachmentsMail(string from, string to, string cc, string subject, string body, string attachments)
        {
            using (MailMessage msgMail = new MailMessage())
            {
                msgMail.To.Add(to.Replace(";", ","));
                msgMail.From = new MailAddress(!String.IsNullOrEmpty(from) ? from : DefaultEmailAddressFrom);
                if (!String.IsNullOrEmpty(cc))
                    msgMail.CC.Add(cc.Replace(";", ","));
                msgMail.Subject = subject;
                msgMail.IsBodyHtml = false;
                msgMail.Body = body;
                msgMail.Priority = MailPriority.High;
                msgMail.Attachments.Add(new Attachment(attachments));

                SmtpClient client = GetSmtpClient();
                try
                {
                    client.Send(msgMail);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

        public static SmtpClient GetSmtpClient()
        {
            SmtpClient client = new SmtpClient(SMTPServer);
            client.Port = SMTPPort;
            client.Host = SMTPHost;
            client.EnableSsl = true;
            client.Credentials = new NetworkCredential(SMTPUser, SMTPPassword);

            return client;
        }
    }
}
